package com.android.jingweiapp.util;

import android.util.Log;

import com.android.jingweiapp.XMLPhase.BaseXMLPhase;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * Created by cuiwenrui-pc on 15/9/16.
 */
public class HttpUtil {

    public static String postWithContent(String url, String content, BaseXMLPhase phase) {
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(6 * 1000);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
            conn.setRequestProperty("Content-Length", String.valueOf(content.getBytes().length));
            OutputStream outStream = conn.getOutputStream();
            outStream.write(content.getBytes());
            outStream.flush();
            outStream.close();
            if (conn.getResponseCode() == 200) {
                // 解析返回信息
                return phase.phaseResponceXML(conn.getInputStream());
            }
            return null;
        } catch (Exception e) {
            Log.e("TTT", "the err is " + e.getMessage());
            return null;
        }
    }

    public static String gettWithContent(String url, BaseXMLPhase phase) {
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(6 * 1000);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
            if (conn.getResponseCode() == 200) {
                // 解析返回信息
                return phase.phaseResponceXML(conn.getInputStream());
            }
            return null;
        } catch (Exception e) {
            Log.e("TTT", "the err is " + e.getMessage());
            return null;
        }
    }

}
